package zmee.pmas.op;

import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.orm.query.QFilter;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

import java.util.List;
import java.util.Objects;

/**
 * @Author: xw
 * @Date: 2023/02/03/15:44
 * @Description: 立项审批提交之前将项目立项上的【项目概算】带到立项审批上
 */
public class ProAuditSubmitOp extends AbstractOperationServicePlugIn {

    @Override
    public void onPreparePropertys(PreparePropertysEventArgs e) {
        List<String> fieldKeys = e.getFieldKeys();
        fieldKeys.add("proapp");
        fieldKeys.add("zmee_projcetbugamt");
        fieldKeys.add("zmee_planbegindate");
        fieldKeys.add("zmee_planenddate");
        fieldKeys.add("zmee_proleader");
        fieldKeys.add("zmee_leaderconttype");
        fieldKeys.add("zmee_isbudget");
        fieldKeys.add("zmee_attachtypeentry");
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        DynamicObject[] dataEntities = e.getDataEntities();
        for (DynamicObject dataEntity : dataEntities) {
            DynamicObject proApp = dataEntity.getDynamicObject("proapp");
            if (Objects.isNull(proApp)) {
                e.setCancelMessage("项目立项为空，请检查！");
                e.setCancel(true);
                return;
            }
                DynamicObject proAppRoval = BusinessDataServiceHelper.loadSingle(proApp.getPkValue(),"pmas_pro_approval");
                dataEntity.set("zmee_projcetbugamt", proAppRoval.getBigDecimal("projcetbugamt"));
                dataEntity.set("zmee_planbegindate", proAppRoval.get("planbegindate"));
                dataEntity.set("zmee_planenddate", proAppRoval.get("planenddate"));
                dataEntity.set("zmee_proleader", proAppRoval.get("proleader"));
                dataEntity.set("zmee_leaderconttype", proAppRoval.get("leaderconttype"));
                dataEntity.set("zmee_isbudget", proAppRoval.get("zmee_combofield2"));

                DynamicObjectCollection appRovalAttachTypeEntry = proAppRoval.getDynamicObjectCollection("attachtypeentry");
                DynamicObjectCollection attachTypeEntry = dataEntity.getDynamicObjectCollection("zmee_attachtypeentry");
                for (DynamicObject object : appRovalAttachTypeEntry) {
                    DynamicObject newDynamic = attachTypeEntry.addNew();
                    newDynamic.set("zmee_attachtype", object.get("attachtype"));
                    DynamicObjectCollection upfile = object.getDynamicObjectCollection("upfile");
                    if (upfile.size()>0) {
                        DynamicObjectCollection upfileNew = newDynamic.getDynamicObjectCollection("zmee_upfile");
                        DynamicObject fileObj = upfileNew.addNew();
                        fileObj.set("fbasedataid", upfile.get(0).get("fbasedataid"));
                        fileObj.set("fbasedataid_id", upfile.get(0).get("fbasedataid_id"));
                    }
                }
             SaveServiceHelper.save(new DynamicObject[]{dataEntity});
            }
    }
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {

    }
}
